//
//  YXMachineMenuView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/23.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXMachineMenuView.h"

@interface YXMachineMenuView ()
@property (nonatomic ,strong) NSArray *titleArr;
@property (nonatomic, strong) UIButton *selectedBtn;
@property (nonatomic, strong) UIView *flagView;
@end
@implementation YXMachineMenuView

- (instancetype)initWithFrame:(CGRect)frame  withTieleArr:(NSArray *)titleArr
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.titleArr = titleArr;
        [self createUI];
    }
    return self;
}
- (void)createUI {
    
    for (int i = 0 ; i < _titleArr.count; i ++ ) {
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        btn.frame = CGRectMake(kScreenWidth / self.titleArr.count * i, 0, kScreenWidth / self.titleArr.count, self.frame.size.height);
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
        [btn setTitle:self.titleArr[i] forState:(UIControlStateNormal)];
        [btn setTitleColor:color_textOne forState:(UIControlStateNormal)];
        [btn setTitleColor:color_blueColor forState:(UIControlStateSelected)];
         btn.tag = 600 + i;
        
        if (i == 0) {
            self.selectedBtn = btn;
            [btn setTitleColor:color_globalColor forState:UIControlStateNormal];
            [self createFlagViewWithFrame:CGRectMake(0, self.frame.size.height-3, kScreenWidth / self.titleArr.count, 3)];
        }
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:btn];
    }
    
    
    for (int i = 0 ; i < self.titleArr.count; i ++) {
        // 分割线
        if (i > 0 && self.frame.size.height > 16) {
            UIView *line = [[UIView alloc] initWithFrame:(CGRectMake(self.W / self.titleArr.count *i, 0, 1, self.frame.size.height))];
            line.backgroundColor = color_lineColor;
            [self addSubview:line];
        }
    }
    
    
    UIView *lineView = [[UIView alloc] initWithFrame:(CGRectMake(0,self.frame.size.height-1, kScreenWidth, 1))];
    lineView.backgroundColor = color_lineColor;
    [self addSubview:lineView];
}

- (void)btnAction:(UIButton *)sender {
    if (self.selectedBtn != sender) {
        
        [sender setTitleColor:color_blueColor forState:UIControlStateNormal];
        [self.selectedBtn setTitleColor:color_textOne forState:UIControlStateNormal];
      
    }
    self.selectedBtn = sender;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.flagView.frame  = CGRectMake(sender.frame.origin.x, sender.frame.size.height - 3, sender.frame.size.width, 3);
    }];
    
    self.selectBtnClick(sender.tag -600);
    
}
- (void)createFlagViewWithFrame:(CGRect)frame  {
    
    self.flagView = [[UIView alloc] initWithFrame:frame];
    self.flagView.backgroundColor = color_blueColor;
    [self addSubview:self.flagView];
    
}

@end
